package org.eweb4j.mvc.validator;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.eweb4j.mvc.config.bean.FieldConfigBean;
import org.eweb4j.mvc.config.bean.ParamConfigBean;
import org.eweb4j.mvc.config.bean.ValidatorConfigBean;


/**
 * 对相同值的验证
 * 
 * @author cfuture.weiwei
 * @since 1.b.8
 * 
 */
public class EqualsValidator implements ValidatorIF {

	public Map<String, String> validate(ValidatorConfigBean val,
			Map<String, String[]> map, HttpServletRequest request) {
		Map<String, String> valError = new HashMap<String, String>();
		for (FieldConfigBean f : val.getField()) {
			String[] value = map.get(f.getName());
			if (value == null || value.length == 0)
				continue;
			String mess = f.getMessage();
			if (mess.length() == 0)
				mess = "请确认填写 %s ";

			String other = null;
			boolean flag = false;
			param: for (ParamConfigBean p : f.getParam()) {
				if (!ValidatorConstant.EQUALS_TO_PARAM
						.equalsIgnoreCase(p.getName()))
					continue;

				String paramValue = p.getValue();
				String[] others = map.get(paramValue);

				if (others == null || others.length == 0) {
					flag = true;
					break param;
				}

				for (String v : value) {
					if (!v.equals(others[0])) {
						other = others[0];
						flag = true;
						break param;
					}
				}
			}

			if (flag && other != null)
				valError.put(f.getName(), String.format(mess, other));

			request.setAttribute(f.getName(), value);
		}
		
		return valError.isEmpty() ? null : valError;
	}

}
